loading libraries

library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

dataset

# Load cleaned crime data
cleaned_crime_data <- read.csv("cleaned_crime_data.csv")

# Ensure the structure of the data
str(cleaned_crime_data)
## 'data.frame':    428 obs. of  6 variables:
##  $ date           : chr  "2012-01-16" "2012-01-26" "2012-01-31" "2012-01-31" ...
##  $ years          : int  2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 ...
##  $ offense        : chr  "Robbery: Armed" "Vandalism" "Cyber Harassment" "Vandalism" ...
##  $ motivation_bias: chr  "Anti-asian" "Anti-lgbtqia" "Anti-black" "Anti-lgbtqia" ...
##  $ district_number: int  9 18 1 24 12 14 20 24 19 18 ...
##  $ community      : chr  "Armour Square" "Lincoln Park" "Near South Side" "West Ridge" ...

Contigency table

# Creating a contingency table
contingency_table <- table(cleaned_crime_data$offense, cleaned_crime_data$district_number, cleaned_crime_data$community)

# Converting the contingency table to a data frame
contingency_df <- as.data.frame.table(contingency_table)

# Renaming the columns
names(contingency_df) <- c("Offense", "District_Number", "Community", "Count")

# Converting District_Number to numeric
contingency_df$District_Number <- as.numeric(contingency_df$District_Number)

# Arranging the District_Number column from smallest to largest
contingency_df <- contingency_df %>%
  arrange(District_Number)

Interactive plot

# Creating an interactive plot
plot <- plot_ly(contingency_df, x = ~District_Number, y = ~Community, z = ~Count, type = "heatmap",
                colorscale = "Viridis", text = ~paste("Offense:", Offense, "<br>District Number:", District_Number, "<br>Community:", Community, "<br>Count:", Count),
                hoverinfo = "text")

plot <- plot %>% layout(title = "Offenses Occurring in Different Areas",
                        xaxis = list(title = "District Number"),
                        yaxis = list(title = "Community"))

# Show plot
plot